package helperClassSearch;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class CreateClimbsPassesMountains {
	public static void CreateHashMapsFile(int[] sheetNumbers, String fileName, String packageName, String className) {

		File replacedFile = new File(fileName);
		replacedFile.delete();

		try {
			Workbook workbook = Workbook.getWorkbook(new File("C:\\Documents and Settings\\Administrator\\My Documents\\Dropbox\\CTA\\XLS\\CTA.xls"));
			FileWriter fstream = new FileWriter(fileName);
			BufferedWriter textFile = new BufferedWriter(fstream);

			textFile.write("package com.cyclingthealps." + packageName + ";\n");
			textFile.write("\n");
			textFile.write("import java.util.HashMap;\n");
			textFile.write("import com.cyclingthealps.client.planned.MyMessagesPlanned;\n");
			textFile.write("import com.cyclingthealps.client.mountains.MyMessagesMountains;");
			textFile.write("import com.cyclingthealps.client.climbs.MyMessagesClimbs;\n");
			textFile.write("import com.google.gwt.core.client.GWT;\n");
			textFile.write("\n");
			textFile.write("			public class ClimbsPassesMountains {\n");
			textFile.write("public static MyMessages res = (MyMessages) GWT.create(MyMessages.class);\n");
			textFile.write("public static MyMessagesPlanned resPlanned = (MyMessagesPlanned) GWT.create(MyMessagesPlanned.class);\n");
			textFile.write("public static MyMessagesMountains resMountains = (MyMessagesMountains) GWT.create(MyMessagesMountains.class);\n");
			textFile.write("public static MyMessagesClimbs resClimbs = (MyMessagesClimbs) GWT.create(MyMessagesClimbs.class);\n");
			textFile.write("\n");

			textFile.write("public static java.util.Map<String, String> kindByName = new HashMap<String, String>();\n");
			textFile.write("static {\n");

			for (int d = 0; d < sheetNumbers.length; d++) {
				Sheet sheet = workbook.getSheet(sheetNumbers[d]);
				Cell[] count = sheet.getColumn(0);
				String resources;
				if (sheetNumbers[d] == 0 || sheetNumbers[d] == 1 || sheetNumbers[d] == 2 || sheetNumbers[d] == 3) {
					resources = "res.";
				} else {
					if (sheetNumbers[d] == 10) {
						resources = "resMountains.";
					} else {
						if (sheetNumbers[d] == 17) {
							resources = "resClimbs.";
						} else {
							resources = "resPlanned.";
						}
					}

				}
				textFile.write("\n");
				for (int i = 1; i < count.length; i++) {
					Cell passLinkCell = sheet.getCell(34, i);
					String passLink = passLinkCell.getContents();
					if (resources.endsWith("resMountains.")) {
						textFile.write("kindByName.put(" + resources + passLink + "(), \"mountains\");\n");
					} else if (resources.endsWith("resClimbs.")) {
						textFile.write("kindByName.put(" + resources + passLink + "(), \"climbs\");\n");
					} else if (sheetNumbers[d] == 3 || sheetNumbers[d] == 7){
						textFile.write("kindByName.put(" + resources + passLink + "(), \"pass\");\n");
					}else {
						textFile.write("kindByName.put(" + resources + passLink + "(), \"passLoaded\");\n");
					}

				}
			}

			textFile.write("}\n");
			textFile.write("}\n");
			textFile.write("\n");
			textFile.close();

		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
